package com.tcgroup.phone.base_core.utils;

import android.content.Context;
import android.util.TypedValue;

/**
 * dp和px换算工具
 */
public class DpPxUtil {
    public static int Dp2Px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density; //当前屏幕密度因子
        return (int) (dp * scale + 0.5f);
    }

    public static int Px2Dp(Context context, float px) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }

    public static int getResourceValueDp(Context context, int ResId) {
        TypedValue typedValue = new TypedValue();
        context.getResources().getValue(ResId,
                typedValue, true);
        return (int) TypedValue.complexToFloat(typedValue.data);
    }

    public static int getResourceValuePx(Context context, int ResId) {
        TypedValue typedValue = new TypedValue();
        context.getResources().getValue(ResId,
                typedValue, true);
        return Dp2Px(context, TypedValue.complexToFloat(typedValue.data));
    }
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }
}
